<?php
// 只有在配置服务器的时候回用到
// 创建一个字符串来存储token值
$token = "happyer";
// 接受微信发送过来的各种参数
$timestamp = $_GET["timestamp"];//时间戳
$nonce = $_GET["nonce"];//随机数

$array = array($token,$timestamp,$nonce);
// 将数组按照字符串进行排序
sort($array,SORT_STRING);
// 将数组合并成字符串
$str = implode($array);
// 对字符串进行加密
$sign = sha1($str);
// 得到传过来的签名
$signature = $_GET["signature"];//签名
// 判断是否来自微信的请求
if ($sign == $signature) {
  echo $_GET['echostr'];
}

// 用户点击按钮的时候会把消息推送到此文件
// 得到原生的post data
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
// 判断是否为空
if (!empty($postStr)) {
  // 检测XML文件是否合法
  libxml_disable_entity_loader(true);
  // 解析XML文件 并把他转化成对象
  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  $msgType = $postObj->MsgType;
  // 对各种事件进行判断
  switch ($msgType) {
    // 假如是event
    case 'event' :
      echo handleEvent($postObj);
      break;
      // 假如是文本
      case 'text' :
      echo handleText($postObj);
      break;
      case 'image' :
      echo handleImage($postObj);
      break;
      case 'voice' :
      echo handleVoice($postObj);
      break;
    default:
      echo "";
      break;
  }
}

  // 处理event事件类型的函数
  function handleEvent ($postObj) {
    $toUserName = $postObj->ToUserName;
    $fromUserName = $postObj->FromUserName;
    // 获取时间戳
    $currentTime = time();
    // 获取事件类型
    $event = $postObj->Event;
    switch ($event) {
      case 'CLICK':
        // 判断他的eventkey类型  （自定义的）
        $eventKey = $postObj->EventKey;
        if ($eventKey == "sendText") {
          $str = <<<AAAA
          <xml>
          <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
          <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
          <CreateTime>$currentTime</CreateTime>
          <MsgType><![CDATA[text]]></MsgType>
          <Content><![CDATA[呀么得]]></Content>
          </xml>
AAAA;
          // 返回给用户
          return $str;
        }else if ($eventKey == "sendImage") {
          // 如果是发送图片的话
          $str = <<<AAAA
          <xml>
          <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
          <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
          <CreateTime>$currentTime</CreateTime>
          <MsgType><![CDATA[image]]></MsgType>
          <Image>
          <MediaId><![CDATA[XJ1DFuqYVLpGWsF7MRy4dNfs5iSN7xtvfuTVCBbKj2Cusi8v5-M2XOgCtjFj-nvK]]></MediaId>
          </Image>
          </xml>
AAAA;
          // 返回给用户
          return $str;
        }else if ($eventKey == "sendVoice") {
          // 如果是发送声音的话
          $str = <<<AAAA
          <xml>
          <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
          <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
          <CreateTime>$currentTime</CreateTime>
          <MsgType><![CDATA[voice]]></MsgType>
          <Voice>
          <MediaId><![CDATA[vNMVYT6lgIREzsb8P6hxMJVowYaNBdygI3CrpWpxRFdYjd0bpvY-7MmJRtVo0iPk]]></MediaId>
          </Voice>
          </xml>
AAAA;
          // 返回给用户
          return $str;
        }
        break;
        // 关注事件
        case 'subscribe' :
        $str = <<<AAAA
        <xml>
        <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
        <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
        <CreateTime>$currentTime</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[welcome to join us , we are 伐木累]]></Content>
</xml>
AAAA;
        return $str;
        break;
        //取消关注事件
        case 'unsubscribe' :
        $str = <<<AAAA
        <xml>
        <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
        <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
        <CreateTime>$currentTime</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[你是谁，为了谁，我的兄弟姐妹不流泪]]></Content>
</xml>
AAAA;
        return $str;
        break;
        // 上报地理位置事件
        case 'LOCATION' :
        $latitude = $postObj->Latitude;
        $longitude = $postObj->Longitude;
        $precision = $postObj->Precision;
        $location = "经度是：".$longitude."纬度是：".$precision;
        $str = <<<AAAA
        <xml>
        <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
        <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
        <CreateTime>$currentTime</CreateTime>
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[$location]]></Content>

        </xml>
AAAA;
        return $str;
        break;
      default:
        # code...
        break;
    }
  }

  // 处理文本类型的函数
  function handleText($postObj) {
    $toUserName = $postObj->ToUserName;
    $fromUserName = $postObj->FromUserName;
    // 获取时间戳
    $currentTime = time();
    $content = $postObj->Content;
    if ($content == "你是猪吗") {
      $content = "你才是猪";
    }else{
      $content = "你个傻叉";
    }

    $str = <<<AAAA
    <xml>
    <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
    <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
    <CreateTime>$currentTime</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[$content]]></Content>
    <MsgId>1234567890123456</MsgId>//匹配消息
    </xml>
AAAA;
    // 返回值
    return $str;
  }

// 处理图片
    function handleImage($postObj){
      $toUserName = $postObj->ToUserName;
      $fromUserName = $postObj->FromUserName;
      // 获取时间戳
      $currentTime = time();
      $mediaId = $postObj->MediaId;
      $picurl = $postObj->PicUrl;

      $str = <<<AAAA
      <xml>
      <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
      <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
      <CreateTime>$currentTime</CreateTime>
      <MsgType><![CDATA[image]]></MsgType>
      <PicUrl><![CDATA[$picUrl]]></PicUrl>
      <Image>
      <MediaId><![CDATA[$mediaId]]></MediaId>
      </Image>
      <MsgId>1234567890123456</MsgId>
      </xml>
AAAA;
      return $str;
    }


    // 处理语音
    function handleVoice($postObj){
      $toUserName = $postObj->ToUserName;
      $fromUserName = $postObj->FromUserName;
      // 获取时间戳
      $currentTime = time();
      $mediaId = $postObj->MediaId;
      $format = $postObj->Format;

      $str = <<<AAAA
      <xml>
      <ToUserName><![CDATA[{$fromUserName}]]></ToUserName>
      <FromUserName><![CDATA[{$toUserName}]]></FromUserName>
      <CreateTime>$currentTime</CreateTime>
      <MsgType><![CDATA[voice]]></MsgType>
      <Voice>
      <MediaId><![CDATA[$mediaId]]></MediaId>
      </Voice>
      <Format><![CDATA[$format]]></Format>
      </xml>
AAAA;
      return $str;
    }



 ?>
